package com.byx.chatclient.im;

import android.util.Log;

import com.byx.chatclient.entity.message.Message;
import com.byx.chatclient.entity.message.MsgBody;
import com.byx.chatclient.event.EventType;
import com.byx.chatclient.event.MessageEvent;
import com.byx.chatclient.http.HttpFilesUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * AbstractMessageSender
 *
 * @author Superb
 * @date 2021/4/10 11:04
 */
public abstract class AbstractMessageSender implements IMessageSender<Message>{
    private static final String TAG = AbstractMessageSender.class.getSimpleName();

    public void sendTextMessage(TextMessageParam messageParam){
        Message message = buildMessage(messageParam.msgBody, messageParam.getSenderId(), messageParam.getTargetId());
        updateView(message);
        execute(message,messageParam.isSingleChat());
    }

    public void sendFileMessage(FileMessageParam messageParam,OnUploadListener onUploadListener){
        List<Message> messageList = new ArrayList<>(messageParam.fileList.size());
        for (File file : messageParam.fileList){
            messageList.add(buildMessage(onUploadListener.beforeUploadConvert(file), messageParam.getSenderId(), messageParam.getTargetId()));
        }
        updateViews(messageList);

        for (int i = 0; i < messageParam.fileList.size(); i++){
            Message message = messageList.get(i);
            HttpFilesUtil.upload(messageParam.fileList.get(i), messageParam.getSenderId(), new HttpFilesUtil.UploadCallBack() {
                @Override
                public void onSuccess(String url) {
                    Log.i(TAG, "onSuccess: " + url);
                    if (onUploadListener != null){
                        onUploadListener.onSucceed(message, url);
                        execute(message,messageParam.isSingleChat());
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    Log.e(TAG, "onFailure: ", t);
                    if (onUploadListener != null){
                        onUploadListener.onFailed(message.getMsgId());
                    }
                }
            },messageParam.fileType);
        }
    }

    /**
     * 执行发送
     *
     * @param message
     * @param isSingleChat
     */
    @Override
    public void execute(Message message, boolean isSingleChat) {
        System.out.println(message);
        MessageProcessor.getInstance().sendMsg(message,isSingleChat);
//        // 发送时通知主页改变数据
//        MessageEvent event = new MessageEvent(isSingleChat ? EventType.SINGLE_CHAT_MSG : EventType.GROUP_CHAT_MSG);
//        event.setData(message);
//        EventBus.getDefault().post(message);
    }

    public interface OnUploadListener{
        MsgBody beforeUploadConvert(File file);
        void onSucceed(Message message, String url);
        void onFailed(String msgId);
    }

    @Data
    public static class MessageParam{
        protected String senderId;
        protected String targetId;
        protected boolean isSingleChat;
    }

    @EqualsAndHashCode(callSuper = true)
    @Builder
    @Data
    public static class TextMessageParam extends MessageParam{
        private MsgBody msgBody;
    }

    @EqualsAndHashCode(callSuper = true)
    @Builder
    @Data
    public static class FileMessageParam extends MessageParam{
        private List<File> fileList;
        private int fileType;
    }
}
